<?php
require_once "db.php";
session_start();

if( !isset($_SESSION['uid']) ){
	echo "<div id='not_logged'>You should login if you want to reservate</div>";
	session_destroy();
	exit();
}
$projid = db_escape($_POST['projid']);
$get_proj_data = "SELECT proj_date, proj_begin, movies_idmovies, halls_idhalls, cinema_idcinema 
						FROM projection 
						WHERE idprojection = '$projid'";
$proj_data_query = db_query($get_proj_data);
while($proj_data_row = mysqli_fetch_assoc($proj_data_query)){
	$idhall = $proj_data_row['halls_idhalls'];
	$idcinema = $proj_data_row['cinema_idcinema'];
	$idmovie = $proj_data_row['movies_idmovies'];
	$proj_date = $proj_data_row['proj_date'];
	$proj_begin = $proj_data_row['proj_begin'];
}

$movie_text_query = "SELECT movie_name, duration FROM movies WHERE idmovies = '$idmovie'";
$movie_query = db_query($movie_text_query);
while ($movie_row = mysqli_fetch_assoc($movie_query)) {
	$moviename = $movie_row['movie_name'];
	$movieduration = $movie_row['duration'];
}

$hall_text_query = "SELECT hall_number, num_of_rows, num_of_col, seats_on_upper_row, seats_on_lower_row, type 
					FROM halls WHERE idhalls = '$idhall'";
$hall_query = db_query($hall_text_query);
while ($hall_row = mysqli_fetch_assoc($hall_query)) {
	$hallnumber = $hall_row['hall_number'];
	$numofrows = $hall_row['num_of_rows'];
	$numofcol = $hall_row['num_of_col'];
	$seats_last_row = $hall_row['seats_on_upper_row'];
	$seats_first_row = $hall_row['seats_on_lower_row'];
	$type_hall = $hall_row['type'];
}

$cinema_text_query = "SELECT cinema_name FROM cinema WHERE idcinema = '$idcinema'";
$cinema_query = db_query($cinema_text_query);
while ($cinema_row = mysqli_fetch_assoc($cinema_query)) {
	$cinemaname = $cinema_row['cinema_name'];
}
$reserveScheme = "<div id = 'reserv_info'><div class='info'> ".$cinemaname."  </div><div class='info'> Hall ".$hallnumber." </div>
	<div class='info'> ".$moviename."  </div><div class='info'> Duration: ".$movieduration." </div>
	<div class='info'> Date: ".$proj_date."  </div><div class='info'> Begining: ".$proj_begin."</div>
	</div><div id = 'hall_scheme'>";
$numofseats = 0;
$reserved = '2';
$free = '0'; 
$checked = '0';

if ($type_hall == '0') {
	$numofseats = $numofrows*$numofcol;
	for ($i=$numofrows; $i >=1 ; $i--) { 
		$reserveScheme .= "<div class='row' data-row='".$i."'><div class='row_number'>Roll ".$i."</div><div class='row_seat'>";
		for ($j=1; $j <= $numofcol ; $j++) { 
			$seat_number = ($i-1)*$numofcol+$j;		
			$seat_query_text = "SELECT * FROM reservations WHERE projection_idprojection = '$projid' AND row ='$i' AND seat = '$j'";
			$seat_query = db_query($seat_query_text);
			 if (mysqli_num_rows($seat_query)=='0') {
			 	$reserveScheme .= "<div class='seat' data-seat='".$seat_number."' data-status='".$free."' style = 'background-color: #009c07'></div>";
			 }
			 else {
			 	$reserveScheme .= "<div class='seat' data-seat='".$seat_number."' data-status='".$reserved."'style = 'background-color: #cc1d1d'></div>";
			 }
		}
		$reserveScheme .= "</div></div>";
	}
}
elseif ($type_hall == '1') {
	for ($k=$seats_last_row; $k > $seats_last_row - $numofrows; $k--) { 
		$numofseats += $k;
	}
	for ($on_that_row = $seats_last_row, $i=$numofrows; $i >=1; $i--, $on_that_row--) { 
		$reserveScheme .= "<div class='row' data-row='".$i."'><div class='row_number'>Roll ".$i."</div><div class='row_seat'>";
		$checked += $on_that_row;
		for ($j=1; $j <= $on_that_row ; $j++) { 
			$seat_number = $numofseats - $checked + $j;
			$seat_query_text = "SELECT * FROM reservations WHERE projection_idprojection = '$projid' AND row ='$i' AND seat = '$j'";
			$seat_query = db_query($seat_query_text);
			if (mysqli_num_rows($seat_query)=='0') {
			 	$reserveScheme .= "<div class='seat' data-seat='".$seat_number."' data-status='".$free."' style = 'background-color: #009c07'></div>";
			 }
			 else {
			 	$reserveScheme .= "<div class='seat' data-seat='".$seat_number."' data-status='".$reserved."'style = 'background-color: #cc1d1d'></div>";
			 }
		}
		$reserveScheme .= "</div></div>";
	}
}

$reserveScheme .= "<div id='screen'>Screen</div><div id='reserve_butt' data-proj='".$projid."'><a href='#'>Reserve</a></div></div>";
echo $reserveScheme;









